package com.coocaa.ops.admin.v1.timedtask;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.timedtask.model.TimedQueryModel;
import com.coocaa.ops.admin.v1.timedtask.service.ITimedTaskQueryService;
import com.coocaa.ops.admin.v1.timedtask.service.ITimedTaskUpdateService;
import com.coocaa.ops.admin.v1.timedtask.service.impl.TimedTaskServiceImpl;
import com.coocaa.ops.admin.v1.timedtask.validator.TimedTaskValidator;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author bijiahao
 * @date : 2018/6/14.
 * @description
 */
@RestController
@RequestMapping("/v1/timedTask")
public class TimedTaskControllerV1 extends MgrBaseController {
    @Autowired
    private TimedTaskServiceImpl timedTaskService;
    @Autowired
    private ITimedTaskQueryService timedTaskQueryService;
    @Autowired
    private ITimedTaskUpdateService timedTaskUpdateService;

    /**
     * 定时任务-取消
     *
     * @param resourceBaseModel
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.AUDIT)
    @RequestMapping(value = "/cancelTimedTask",  method = {RequestMethod.GET, RequestMethod.POST})
    public Object cancelTask(ResourceBaseModel resourceBaseModel, HttpServletRequest request) {
        InputParamsValidator.validIdAndVersionAndType(resourceBaseModel);
        return timedTaskService.cancelTimedTask(resourceBaseModel, super.getCurrentUserId(request));
    }

    /**
     * 获取定时信息-根据ID-版本-类型
     *
     * @param id
     * @param version
     * @param type
     * @return
     */
    @RequestMapping(value = "/getTimedInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public Object getTimedInfo(Integer id, Integer version, String type) {
        InputParamsValidator.validIdAndVersionAndType(id, version, type);
        Object data = timedTaskService.getTimedInfo(id, version, type);
        return R.ok().data(data);
    }

    /**
     * 定时数量限制
     *
     * @return
     */
    @RequestMapping(value = "/taskLimit", method = {RequestMethod.GET, RequestMethod.POST})
    public Object taskLimit() {
        return timedTaskService.taskLimit();
    }


    /**
     * 分页数据显示-菜单自己使用
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String pageList(HttpServletRequest request, TimedQueryModel model) {
        PageResultModel<TimedTaskInfoEntity> pageResult = timedTaskQueryService.pageList(model);
        R result = R.ok(pageResult);
        return render2Json(result);
    }

    /**
     * 保存Job
     *
     * @param request
     * @param jsonStr
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public R saveJob(HttpServletRequest request, @RequestParam String jsonStr) throws Exception {
        TimedTaskInfoEntity entity = (TimedTaskInfoEntity) JsonUtil.json2Object(jsonStr, TimedTaskInfoEntity.class);
        TimedTaskValidator.valid(entity);
        return timedTaskUpdateService.saveJob(entity);
    }

    /**
     * 恢复Job
     *
     * @param request
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/resumeJob",method = {RequestMethod.GET, RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public R resumeJob(HttpServletRequest request, @RequestParam Integer id) throws Exception {
        return timedTaskUpdateService.resumeJob(id);
    }

    /**
     * 停止job
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/stopJob",method = {RequestMethod.GET, RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public R stopJob(HttpServletRequest request, @RequestParam Integer id) {
        return timedTaskUpdateService.stopJob(id);
    }

    /**
     * 删除job
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/delJob", method = {RequestMethod.GET, RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public R delJob(HttpServletRequest request, @RequestParam Integer id) {
        return timedTaskUpdateService.delJob(id);
    }

    /**
     * 获取调度器信息
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getQuartzSchedulerInfo",method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public R getQuartzSchedulerInfo(HttpServletRequest request, Integer id) throws Exception {
        return timedTaskQueryService.getQuartzSchedulerInfo();
    }

    @RequestMapping(value = "/getDetailInfo", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public R getDetailInfo(@RequestParam Integer id) {
        return timedTaskQueryService.getDetailInfo(id);
    }


}
